package file;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;

public class FileMenuAdapter {
	private String suffix = ".appointment";
	
	private IFileConnection connection;
	private File file;
	private JFrame frame;
	private JFileChooser fileChooser;
	
	public FileMenuAdapter(JFrame frame, IFileConnection connection){
		this.frame = frame;
		this.connection = connection;
		initFileChooser();
		addMenus();
	}
	
	private void initFileChooser(){
		fileChooser = new JFileChooser("./files");
		fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		fileChooser.setFileFilter(new FileFilter() {
			
			@Override
			public String getDescription() {
				return "Appointments";
			}
			
			@Override
			public boolean accept(File arg0) {
				if(arg0.isFile())
					return arg0.getName().endsWith(suffix);
				return true;
			}
		});
	}
	
	private void addMenus(){
		JMenuBar mbar;
		if(frame.getJMenuBar() != null)
			mbar = frame.getJMenuBar();
		else
			mbar = new JMenuBar();
		JMenu fileMenu = new JMenu("File");
		JMenuItem newFile = new JMenuItem("new");
		JMenuItem openFile = new JMenuItem("open");
		JMenuItem saveFile = new JMenuItem("save");
		JMenuItem saveAsFile = new JMenuItem("save as...");
		JMenuItem close = new JMenuItem("close");
		mbar.add(fileMenu,0);
		fileMenu.add(newFile);
		fileMenu.add(openFile);
		fileMenu.add(saveFile);
		fileMenu.add(saveAsFile);
		fileMenu.add(close);
		
		newFile.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				newFile();
			}
		});
		
		openFile.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				open();
			}
		});
		
		saveFile.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				save();
			}
		});
		
		saveAsFile.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				saveAs();
			}
		});
		
		close.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				close();
			}
		});
	}
	
	protected boolean saveChanges(){
		if(connection.changes()){
			switch(JOptionPane.showConfirmDialog(frame, "save changes?"))
			{
			case JOptionPane.YES_OPTION:
				return save();
			case JOptionPane.CANCEL_OPTION:
				return false;
			case JOptionPane.NO_OPTION:
				return true;
			}
			return false;
		}
		else
			return true; //no changes
	}
	
	protected boolean newFile(){
		if (saveChanges()){
			try{
				connection.newFile();
				file = null;
				return true;
			} catch(IOException e){
				JOptionPane.showMessageDialog(frame, e.getMessage());
				return false;
			}
		}
		else
			return false;
	}
	
	protected boolean save(){
		if(file != null){
			try{
				connection.save(file);
				return true;
			} catch(IOException e){
				JOptionPane.showMessageDialog(frame, e.getMessage());
				return false;
			}
		}
		else
			return saveAs();
	}
	
	protected boolean saveAs(){
		boolean fileOK = false;
		while(!fileOK){
			if(fileChooser.showSaveDialog(frame) == JFileChooser.APPROVE_OPTION){
				File f = fileChooser.getSelectedFile();
				if(!f.getName().endsWith(suffix))
					f = new File(f.getAbsolutePath() + suffix);
				if(f.exists()){
					switch (JOptionPane.showConfirmDialog(frame, "override existing file?")) {
					case JOptionPane.YES_OPTION:
						fileOK = true;
						file = f;
						break;
					
					case JOptionPane.CANCEL_OPTION:
						return false;
						
					case JOptionPane.NO_OPTION:
						break;
					}
				}
				else{
					file = f;
					fileOK = true;
				}
			}
			else
				return false;
		}
		
		return save();
	}
	
	protected boolean open(){
		if(!saveChanges())
			return false;
		if(fileChooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION){
			file = fileChooser.getSelectedFile();
			try{
				connection.open(file);
				return true;
			} catch(IOException e){
				JOptionPane.showMessageDialog(frame, e.getMessage());
			}
		}
		return false;
	}
	
	protected void close(){
		if(saveChanges())
			System.exit(0);
	}
}
